﻿//
// Copyright (c) 2011 Western Data Solutions (www.westerndatasolutions.com) (Armen Shimoon)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without 
// limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 
// the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 
// OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
namespace WDS.Bencode.Torrents
{
    /// <summary>
    /// A Torrent file is actually a bencoded dictionary with a defined set of key-value pairs
    /// </summary>
    public class Torrent : BencodeDictionary
    {

        public static Torrent ReadFromFile(string filePath)
        {
            var dom = System.IO.File.ReadAllText(filePath).ToBencodeDictionaryElement();
            return new Torrent(dom);
        }

        public Torrent()
        {
                
        }

        /// <summary>
        /// Allow constructing a Torrent file from a bencoded dictionary
        /// </summary>
        /// <param name="values">The bencoded dictionary</param>
        public Torrent(BencodeDictionary values)
        {
            // Add each key value pair to the torrent file
            foreach (var keyValuePair in values)
            {
                Add(keyValuePair);
            }

            // Construct strongly typed torrent representation of the "info" dictionary
            var info = ValueOfType<BencodeDictionary>("info");
            if (!IsNull(info))
            {
               SetValue("info", new TorrentInfoHeader(info)); 
            }

            // Construct strongly typed torrent representation of the "nodes" list
            var nodes = ValueOfType<BencodeList<TorrentNode>>("nodes");
            if (!IsNull(nodes))
            {
              SetValue("nodes", new TorrentNodes(nodes));  
            }

            // Construct strongtly typed torrent representation of the "announce-list" list
            var announceList = ValueOfType<BencodeList>("announce-list");
            if (!IsNull(announceList))
            {
              SetValue("announce-list", new TorrentAnnounceList(announceList));  
            }
            
        }

        /// <summary>
        /// Torrent.encoding
        /// </summary>
        public string Encoding
        {
            get { return ValueOfType<BencodeString>("encoding").Value; }
            set { SetValue("encoding", value); }
        }

        /// <summary>
        /// Torrent."creation date"
        /// </summary>
        public int CreationDate
        {
            get { return ValueOfType<BencodeInteger>("creation date").Value; }
            set { SetValue("creation date", value); }
        }

        /// <summary>
        /// Torrent."created by"
        /// </summary>
        public string CreatedBy
        {
            get { return ValueOfType<BencodeString>("created by").Value; }
            set { SetValue("created by", value); }
        }

        /// <summary>
        /// Torrent."announce"
        /// </summary>
        public string Announce
        {
            get { return ValueOfType<BencodeString>("announce").Value; }
            set { SetValue("announce", value); }
        }
        
        /// <summary>
        /// Torrent.private
        /// </summary>
        public int Private
        {
            get { return ValueOfType<BencodeInteger>("private").Value; }
            set { SetValue("private", value); }
        }

        /// <summary>
        /// Torrent."announce-list"
        /// </summary>
        public TorrentAnnounceList AnnounceList
        {
            get { return ValueOfType<TorrentAnnounceList>("announce-list");  }
            set { SetValue("announce-list", value);}
        }

        /// <summary>
        /// Torrent.nodes
        /// </summary>
        public TorrentNodes Nodes
        {
            get { return ValueOfType<TorrentNodes>("nodes"); }
            set { SetValue("nodes", value);  }
        }

        /// <summary>
        /// Torrent.info
        /// </summary>
        public TorrentInfoHeader Info
        {
            get { return ValueOfType<TorrentInfoHeader>("info"); }
            set { SetValue("info", value);}
        }
      
    }
 
}
